/* Copyright © 2010 LPRM
Universidade Federal do Espírito Santo - RNP-GT CWTools

This file is part of JoinUs!.

JoinUs! is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

JoinUs! is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with JoinUs!. If not, see <http://www.gnu.org/licenses/>. */
package br.ufes.cwtools.joinus.profile_manager.ui_builders;

import java.util.Calendar;
import java.util.Vector;

import br.ufes.cwtools.joinus.profile_manager.profile.General;
import br.ufes.cwtools.joinus.profile_manager.profile.Profile;

import android.app.DatePickerDialog;
import br.ufes.cwtools.joinus.R;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Spinner;
import android.widget.TextView;

/*
 * this class does all the programatic UI work
 * it is also responsable for exporting a object containing the data
 * acquired from the UI  
 */
public class GeneralUI extends ProfileUI {
	boolean changed = false;
	View myView;
	int selectedYear = 0;
	int selectedMonth = 0;
	int selectedDay = 0;
	Vector<String> iSpeak = new Vector<String>();
	Vector<Integer> iSpeakId = new Vector<Integer>();
	Spinner relation, country, languages;

	@Override
	public void importPro(Profile p) {
		General g = (General) p;
		((EditText) myView.findViewById(R.id.first_name_text)).setText(g.firstName);
		((EditText) myView.findViewById(R.id.last_name_text)).setText(g.lastName);
		((RadioButton) myView.findViewById(R.id.gender_male)).setChecked(g.isMale);
		((RadioButton) myView.findViewById(R.id.gender_female)).setChecked(!g.isMale);
		relation.setSelection(g.relationshipStatus);
		selectedYear = g.birthYear;
		selectedMonth = g.birthMonth;
		selectedDay = g.birthDay;
		if (g.birthYear != 0) {
			EditText year = (EditText) myView.findViewById(R.id.birth_year_chooser);
			year.setText(selectedYear + "");
		}
		if (selectedDay != 0) {
			TextView msg = (TextView) myView.findViewById(R.id.year_display);
			msg.setText(selectedMonth+"/"+selectedDay);
		}
		((EditText) myView.findViewById(R.id.city_text)).setText(g.city);
		((EditText) myView.findViewById(R.id.state_text)).setText(g.state);
		((EditText) myView.findViewById(R.id.zip_text)).setText(g.zipCode);
		country.setSelection(g.country);
		((EditText) myView.findViewById(R.id.high_school_text)).setText(g.highSchool);
		((EditText) myView.findViewById(R.id.college_text)).setText(g.college);
		((EditText) myView.findViewById(R.id.company_text)).setText(g.company);		
		iSpeakId= new Vector<Integer>();
		for (int i = 0; i < g.language.length; i++) {
			iSpeakId.add(g.language[i]);
		}
		final TextView t = (TextView) myView.findViewById(R.id.languages_list1);
		t.setText("");
		for (String s : iSpeak) {
			t.append(s);
		}
		((CheckBox) myView.findViewById(R.id.friends_check)).setChecked(g.interests[0]);
		((CheckBox) myView.findViewById(R.id.activities_check)).setChecked(g.interests[1]);
		((CheckBox) myView.findViewById(R.id.business_check)).setChecked(g.interests[2]);
		((CheckBox) myView.findViewById(R.id.dating_check)).setChecked(g.interests[3]);
	}
	
	public GeneralUI() {
		layoutID = R.layout.general;
	}

	private void SetUpSpinners(View v) {
		Spinner s = relation = (Spinner) v.findViewById(R.id.relationship_chooser);

		ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
			v.getContext(), R.array.relationship, android.R.layout.simple_spinner_item);
		s.setAdapter(adapter);

		country = s = (Spinner) v.findViewById(R.id.country_chooser);
		adapter = ArrayAdapter.createFromResource(v.getContext(),
				R.array.countries, android.R.layout.simple_spinner_item);
		s.setAdapter(adapter);

		languages = s = (Spinner) v.findViewById(R.id.languages_chooser);
		adapter = ArrayAdapter.createFromResource(v.getContext(),
				R.array.languages, android.R.layout.simple_spinner_item);
		s.setAdapter(adapter);
	}

	public void SetUpCalendar(View v) {
		Button b = (Button) v.findViewById(R.id.birth_month_chooser);
		b.setOnClickListener(new OnClickListener() {
			EditText year = (EditText) GeneralUI.this.myView.findViewById(R.id.birth_year_chooser);
			TextView msg = (TextView) GeneralUI.this.myView.findViewById(R.id.year_display);

			public void onClick(View view) {
				final Calendar c = Calendar.getInstance();
				int currentYear = c.get(Calendar.YEAR);
				boolean ex = false;
				int numYear = 0;
				final DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {
					//public void dateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
					public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
						selectedYear = year;
						selectedMonth = monthOfYear + 1;
						selectedDay = dayOfMonth;
						msg.setText(selectedMonth + "/" + selectedDay);
					}
				};

				try {
					numYear = Integer.parseInt(year.getText().toString());
				} catch (NumberFormatException e) {
					ex = true;
				}
				
				if (ex || numYear < currentYear - 150 || numYear > currentYear) {
				/*	NotificationManager nm = (NotificationManager) myView
							.getContext().getSystemService(
									Context.NOTIFICATION_SERVICE);
					nm.notifyWithText(1, "Please, input a valid birth year.",
							NotificationManager.LENGTH_LONG, null);*/
					year.setText("");
					year.requestFocus();
				} else {
					new DatePickerDialog(myView.getContext(), mDateSetListener, numYear, 0, 1); //	new DatePickerDialog(myView.getContext(), mDateSetListener, numYear, 0, 1, Calendar.SUNDAY).show();			
				}
			}
		});
	}

	private void SetUpLanguages(View v) {
		final TextView t = (TextView) v.findViewById(R.id.languages_list1);
		final Spinner s = (Spinner) v.findViewById(R.id.languages_chooser);
		Button add = (Button) v.findViewById(R.id.add_language);
		add.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				if (iSpeak.contains(s.getSelectedItem() + "\n")) {
					iSpeak.remove(s.getSelectedItem() + "\n");
					iSpeakId.remove(Integer.valueOf(s.getSelectedItemPosition()));
					if (iSpeak.isEmpty())
						t.setText("none provided");
				} else {
					iSpeak.add(s.getSelectedItem() + "\n");
					iSpeakId.add(Integer.valueOf(s.getSelectedItemPosition()));
				}
				t.setText("");
				for (String s : iSpeak) {
					t.append(s);
				}
			}
		});
	}

	public void SetUpPinPoint() {
		// TODO: return the coordinates
	/*	Button add = (Button) myView.findViewById(R.id.pinpoint_buton);
		add.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				//Intent map = new Intent(myView.getContext(),MyMap.class);
				//((Activity)myView.getContext()).startSubActivity(map, 0);
			}
		});*/
	}

	public void setUpWidgets(View v) {
		changed = true;
		myView = v;
		SetUpSpinners(v);
		SetUpCalendar(v);
		SetUpLanguages(v);
		SetUpPinPoint();
	}

	public String toString() {
		return "General";
	}

	@Override
	public Profile export() {
		if (changed) {
			General general = new General();
			if (((EditText) myView.findViewById(R.id.first_name_text)) != null) {
				general.firstName = ((EditText) myView.findViewById(R.id.first_name_text)).getText().toString();
				general.lastName = ((EditText) myView.findViewById(R.id.last_name_text)).getText().toString();
				general.isMale = ((RadioButton) myView.findViewById(R.id.gender_male)).isChecked();
				general.relationshipStatus = relation.getSelectedItemPosition();
				general.birthYear = selectedYear;
				general.birthMonth = selectedMonth;
				general.birthDay = selectedDay;
				general.city = ((EditText) myView.findViewById(R.id.city_text)).getText().toString();
				general.state = ((EditText) myView.findViewById(R.id.state_text)).getText().toString();
				general.zipCode = ((EditText) myView.findViewById(R.id.zip_text)).getText().toString();
				general.country = country.getSelectedItemPosition();
				general.highSchool = ((EditText) myView.findViewById(R.id.high_school_text)).getText().toString();
				general.college = ((EditText) myView.findViewById(R.id.college_text)).getText().toString();
				general.company = ((EditText) myView.findViewById(R.id.company_text)).getText().toString();
				general.language = new int[iSpeakId.size()];
				int i = 0;
				for (Integer s : iSpeakId) {
					general.language[i] = s;
					i++;
				}
				general.interests[0] = ((CheckBox) myView.findViewById(R.id.friends_check)).isChecked();
				general.interests[1] = ((CheckBox) myView.findViewById(R.id.activities_check)).isChecked();
				general.interests[2] = ((CheckBox) myView.findViewById(R.id.business_check)).isChecked();
				general.interests[3] = ((CheckBox) myView.findViewById(R.id.dating_check)).isChecked();
				return general;
			} else {
				return null;
			}
		} else {
			return null;
		}
	}
}
